/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: Contrast
 * Author:   zhouheng
 * Date:     2018/6/23 17:05
 * Description: 两个对象对比工具类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.core.util;

import com.hengby.hengcrud.core.common.dictmap.base.AbtractDictMap;
import com.hengby.hengcrud.core.common.dictmap.factory.DictFieldWarpperFactory;
import com.hengby.hengcrud.core.common.factory.ConstantFactory;
import com.hengby.hengcrud.core.common.factory.IConstantFactory;
import com.hengby.hengcrud.core.support.StrKit;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map; /**
 * 〈一句话功能简述〉<br> 
 * 〈两个对象对比工具类〉
 *
 * @author zhouheng
 * @create 2018/6/23
 * @since 1.0.0
 */
public class Contrast {
    //记录每个修改字段的分隔符
    public static final String separator = ";;;";

    /**
     * 解析多个key(逗号隔开的)
     */
    public static String parseMutiKey(AbtractDictMap dictMap,
                                      String key, Map<String, String> parameters) {

        StringBuilder sb = new StringBuilder();
        if (-1 != key.indexOf(",")){
            String[] keyArr = key.split(",");

            for (String s : keyArr){
                String dictMethodName = dictMap.fieldWarpperDictory.get(s);
                Object value = parameters.get(s);
                if (dictMethodName != null){
                    IConstantFactory constantFactory = ConstantFactory.me();
                    Object object = DictFieldWarpperFactory.createFieldWarpper(value,dictMethodName);

                    sb.append(dictMap.dictory.get(s) + "=" + object);
                }else {
                    sb.append(dictMap.dictory.get(s) + "=" + value);
                }
            }

            return sb.toString();
        }else {
            String dictMethodName = dictMap.fieldWarpperDictory.get(key);
            Object value = parameters.get(key);

            if (dictMethodName != null){
                IConstantFactory constantFactory = ConstantFactory.me();
                Object object = DictFieldWarpperFactory.createFieldWarpper(value,dictMethodName);

                sb.append(dictMap.dictory.get(key) + "=" + object);
            }else {
                sb.append(dictMap.dictory.get(key) + "=" + value);
            }

            return sb.toString();
        }
    }

    /**
     * 比较两个对象不同
     * @return
     */
    public static String contrastObj(Class dictClass, String key, Object obj1, Map<String,String> obj2) throws IntrospectionException, InvocationTargetException, IllegalAccessException, InstantiationException {

        AbtractDictMap abtractDictMap = (AbtractDictMap) dictClass.newInstance();
        String str = parseMutiKey(abtractDictMap,key,obj2) + separator;

        Class clazz = obj1.getClass();
        Field[] fields = clazz.getDeclaredFields();

        int i = 0;
        for (Field field : fields){
            PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
            Method readMethod = pd.getReadMethod();
            Object o1 = readMethod.invoke(obj1);
            Object o2 = obj2.get(StrKit.firstCharToLowerCase(readMethod.getName().substring(3)));

            if (o1 == null || o2 == null){
                continue;
            }else if (o1 instanceof Date){
                o1 = DateUtil.getDay((Date) o1);
            }else if (o1 instanceof Integer){
                o2 = Integer.parseInt(o2.toString());
            }

            // o1 o2 不相等
            if (!o1.toString().equals(o2.toString())){
                if (i != 0){
                    str += separator;
                }

                String fieldName = abtractDictMap.dictory.get(field.getName());
                String fieldWarpperMethodName = abtractDictMap.fieldWarpperDictory.get(field.getName());
                if (fieldWarpperMethodName != null){
                    Object o1Warpper = DictFieldWarpperFactory.createFieldWarpper(o1, fieldWarpperMethodName);
                    Object o2Warpper = DictFieldWarpperFactory.createFieldWarpper(o2, fieldWarpperMethodName);
                    str += "字段名称:" + fieldName + ",旧值:" + o1Warpper + ",新值:" + o2Warpper;
                }else {
                    str += "字段名称:" + fieldName + ",旧值:" + o1 + ",新值:" + o2;
                }
                i++;
            }

        }

        return str;
    }
}